/*
 * ServerMonitor.java
 *
 * Created on April 14, 2007, 9:37 PM
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.web;

import java.util.Calendar;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;
import org.fpdev.core.FPEngine;

/**
 *
 * @author demory
 */
public class ServerMonitor {

  private FPEngine engine_;
  private MonitorableServer server_;
  private Runtime runtime_;
  private Timer timer_;

  public ServerMonitor(MonitorableServer server, FPEngine engine, int ms) {
    server_ = server;
    engine_ = engine;
    runtime_ = Runtime.getRuntime();
    timer_ = new Timer(); //500, this);
    //timer_.setRepeats(false);
    timer_.scheduleAtFixedRate(new TimerTask() {

      public void run() {
        timerCall();
      }
    }, 1000, ms);
  }

  public void timerCall() {
    System.out.println("");
    System.out.println("-- begin ServerMonitor call --");

    Calendar cal = Calendar.getInstance(TimeZone.getDefault());
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setTimeZone(TimeZone.getDefault());
    System.out.println("Time: " + sdf.format(cal.getTime()));

    System.gc();
    System.out.println("Garbage collected, current mem usage: " + (runtime_.totalMemory() - runtime_.freeMemory()));

    engine_.getDataPackage().getCoreDB().cleanup();
    System.out.println("Cleaned up CoreDB");
    
    engine_.getDataPackage().getCoreDB().ping();
    System.out.println("Pinged database");

    server_.monitor();

    System.out.println("-- end ServerMonitor call --");
  }
}
